@using System.Collections
<DocMatSortHeader></DocMatSortHeader>
<DocMatSortHeaderRow></DocMatSortHeaderRow>

<h5 class="mat-h5">Example</h5>

<DemoContainer >
    <Content>
        <table>
            <MatSortHeaderRow Class="demo-h-row" SortChanged="@SortData">
                <MatSortHeader Class="demo-h" SortId="name">Dessert (100g)</MatSortHeader>
                <MatSortHeader Class="demo-h" SortId="calories">Calories</MatSortHeader>
                <MatSortHeader Class="demo-h" SortId="fat">Fat (g)</MatSortHeader>
                <MatSortHeader Class="demo-h" SortId="carbs">Carbs (g)</MatSortHeader>
                <MatSortHeader Class="demo-h" SortId="protein">Protein (g)</MatSortHeader>
            </MatSortHeaderRow>

            @foreach (var dessert in sortedData)
            {
                <tr>
                    <td>@dessert.Name</td>
                    <td>@dessert.Calories</td>
                    <td>@dessert.Fat</td>
                    <td>@dessert.Carbs</td>
                    <td>@dessert.Protein</td>
                </tr>
            }


        </table>

        @code
        {
            class Dessert
            {
                public int Calories { get; set; }
                public int Carbs { get; set; }
                public int Fat { get; set; }
                public string Name { get; set; }
                public int Protein { get; set; }
            }

            Dessert[] desserts = new[]
            {
                new Dessert() {Name = "Frozen yogurt", Calories = 159, Fat = 6, Carbs = 24, Protein = 4},
                new Dessert() {Name = "Ice cream sandwich", Calories = 237, Fat = 9, Carbs = 37, Protein = 4},
                new Dessert() {Name = "Eclair", Calories = 262, Fat = 16, Carbs = 24, Protein = 6},
                new Dessert() {Name = "Cupcake", Calories = 305, Fat = 4, Carbs = 67, Protein = 4},
                new Dessert() {Name = "Gingerbread", Calories = 356, Fat = 16, Carbs = 49, Protein = 4},
            };

            void SortData(MatSortChangedEvent sort)
            {
                sortedData = desserts.ToArray();
                if (!(sort == null || sort.Direction == MatSortDirection.None || string.IsNullOrEmpty(sort.SortId)))
                {
                    Comparison<Dessert> comparison = null;
                    switch (sort.SortId)
                    {
                        case "name":
                            comparison = (s1, s2) => string.Compare(s1.Name, s2.Name, StringComparison.InvariantCultureIgnoreCase);
                            break;
                        case "calories":
                            comparison = (s1, s2) => s1.Calories.CompareTo(s2.Calories);
                            break;
                        case "fat":
                            comparison = (s1, s2) => s1.Fat.CompareTo(s2.Fat);
                            break;
                        case "carbs":
                            comparison = (s1, s2) => s1.Carbs.CompareTo(s2.Carbs);
                            break;
                        case "protein":
                            comparison = (s1, s2) => s1.Protein.CompareTo(s2.Protein);
                            break;
                    }
                    if (comparison != null)
                    {
                        if (sort.Direction == MatSortDirection.Desc)
                        {
                            Array.Sort(sortedData, (s1, s2) => -1 * comparison(s1, s2));
                        }
                        else
                        {
                            Array.Sort(sortedData, comparison);
                        }

                    }
                }
            }

            Dessert[] sortedData = null;

            protected override void OnInitialized()
            {
                base.OnInitialized();
                SortData(null);
            }
        }

    </Content>
    <SourceContent>
    	<BlazorFiddle Template="MatBlazor" Code=@(@"
        <table>
            <MatSortHeaderRow Class=""demo-h-row"" SortChanged=""@SortData"">
                <MatSortHeader Class=""demo-h"" SortId=""name"">Dessert (100g)</MatSortHeader>
                <MatSortHeader Class=""demo-h"" SortId=""calories"">Calories</MatSortHeader>
                <MatSortHeader Class=""demo-h"" SortId=""fat"">Fat (g)</MatSortHeader>
                <MatSortHeader Class=""demo-h"" SortId=""carbs"">Carbs (g)</MatSortHeader>
                <MatSortHeader Class=""demo-h"" SortId=""protein"">Protein (g)</MatSortHeader>
            </MatSortHeaderRow>

            @foreach (var dessert in sortedData)
            {
                <tr>
                    <td>@dessert.Name</td>
                    <td>@dessert.Calories</td>
                    <td>@dessert.Fat</td>
                    <td>@dessert.Carbs</td>
                    <td>@dessert.Protein</td>
                </tr>
            }


        </table>

        @code
        {
            class Dessert
            {
                public int Calories { get; set; }
                public int Carbs { get; set; }
                public int Fat { get; set; }
                public string Name { get; set; }
                public int Protein { get; set; }
            }

            Dessert[] desserts = new[]
            {
                new Dessert() {Name = ""Frozen yogurt"", Calories = 159, Fat = 6, Carbs = 24, Protein = 4},
                new Dessert() {Name = ""Ice cream sandwich"", Calories = 237, Fat = 9, Carbs = 37, Protein = 4},
                new Dessert() {Name = ""Eclair"", Calories = 262, Fat = 16, Carbs = 24, Protein = 6},
                new Dessert() {Name = ""Cupcake"", Calories = 305, Fat = 4, Carbs = 67, Protein = 4},
                new Dessert() {Name = ""Gingerbread"", Calories = 356, Fat = 16, Carbs = 49, Protein = 4},
            };

            void SortData(MatSortChangedEvent sort)
            {
                sortedData = desserts.ToArray();
                if (!(sort == null || sort.Direction == MatSortDirection.None || string.IsNullOrEmpty(sort.SortId)))
                {
                    Comparison<Dessert> comparison = null;
                    switch (sort.SortId)
                    {
                        case ""name"":
                            comparison = (s1, s2) => string.Compare(s1.Name, s2.Name, StringComparison.InvariantCultureIgnoreCase);
                            break;
                        case ""calories"":
                            comparison = (s1, s2) => s1.Calories.CompareTo(s2.Calories);
                            break;
                        case ""fat"":
                            comparison = (s1, s2) => s1.Fat.CompareTo(s2.Fat);
                            break;
                        case ""carbs"":
                            comparison = (s1, s2) => s1.Carbs.CompareTo(s2.Carbs);
                            break;
                        case ""protein"":
                            comparison = (s1, s2) => s1.Protein.CompareTo(s2.Protein);
                            break;
                    }
                    if (comparison != null)
                    {
                        if (sort.Direction == MatSortDirection.Desc)
                        {
                            Array.Sort(sortedData, (s1, s2) => -1 * comparison(s1, s2));
                        }
                        else
                        {
                            Array.Sort(sortedData, comparison);
                        }

                    }
                }
            }

            Dessert[] sortedData = null;

            protected override void OnInitialized()
            {
                base.OnInitialized();
                SortData(null);
            }
        }

    ")></BlazorFiddle>
    </SourceContent>
</DemoContainer>